
//
//  StatusListViewModel.swift
//  Weibo
//
//  Created by 张婷 on 15/10/23.
//  Copyright © 2015年 itheima. All rights reserved.
//

import Foundation

class StatusListViewModel {
    
    /// 微博列表
    lazy var statusList = [StatusViewModel]()
    
    ///  加载网络微博数据
    ///
    ///  - parameter finished: 回调传递
    func loadStatus(finished: (isSuccess: Bool) -> ()) {
        
        NetworkTools.sharedTools.loadStatus { (result, error) -> () in
            
            // 1. 判断网络是否有错
            if error != nil {
                finished(isSuccess: false)
                print("网路不给力")
                return
            }
            
            // 2. 判断数据格式
            guard let dictList = result?["statuses"] as? [[String: AnyObject]] else {
                finished(isSuccess: false)
                print("数据格式有错")
                return
            }
            
            // 3. 将字典数组转化为模型数组
            var arrayList = [StatusViewModel]()
            
            for dict in dictList {
                arrayList.append(StatusViewModel(status: Status(dict: dict)))
            }
            
            self.statusList = arrayList + self.statusList
            
            // 4. 添加成功回调
            finished(isSuccess: true)
        }
    }
}
